package cn.xshi.oauth.web;

import cn.xshi.oauth.form.OauthAccountTypeForm;
import cn.xshi.oauth.model.OauthAccountType;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.idgeneration.UUID;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthAccountTypeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 授权中心账户类型API
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthAccountType")
@Api(value = "授权中心账户类型API",tags = "授权中心账户类型API",description = "授权中心账户类型API")
public class OauthAccountTypeController extends BaseAction {

    @Resource
    OauthAccountTypeService oauthAccountTypeService;

    /**
     * 加载初始化列表数据并分页
     * @param oauthAccountTypeForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="查询账户类型集合并分页", notes="查询账户类型集合并分页")
    @GetMapping(value="/list")
    @Auth(value = "/oauthAccountType/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<OauthAccountType>> getOauthAccountTypeList(OauthAccountTypeForm oauthAccountTypeForm,Integer pageNo, Integer pageSize){
        commonHPagerByPageNo(pageNo,pageSize);
        List<OauthAccountType> oauthAccountTypeList = oauthAccountTypeService.getOauthAccountTypeList(oauthAccountTypeForm);
        PageInfo<OauthAccountType> page = new PageInfo<OauthAccountType>(oauthAccountTypeList);
        return outPageBootByPageNo(page,pageNo,pageSize);
    }

    /**
     * 查询单个账户类型
     * @param id
     */
    @GetMapping(value="/get/{id}")
    @ApiOperation(value="查询单个账户类型", notes="根据Id查找对象")
    @Auth(value = "/oauthAccountType/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<OauthAccountType> getOauthAccountTypeById(@PathVariable("id")String id){
        OauthAccountType oauthAccountType = oauthAccountTypeService.getOauthAccountTypeById(id);
        if(!StringUtil.isEmpty(oauthAccountType.getCreateId())){
            OauthAccountEntity createBy = getAccount(oauthAccountType.getCreateId());
            if(null != createBy){
                oauthAccountType.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(oauthAccountType.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(oauthAccountType.getUpdateId());
            if(null != modifiedBy){
                oauthAccountType.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(oauthAccountType);
    }

    /**
     * 添加
     * @param oauthAccountType
     */
    @PostMapping(value="/add")
    @ApiOperation(value="创建单个账户类型", notes="创建单个账户类型")
    public BaseResult addOauthAccountType(@RequestBody OauthAccountType oauthAccountType){
        int i = 0;
        if(null != oauthAccountType){
            oauthAccountType.setId(UUID.toUUID());
            i=oauthAccountTypeService.addOauthAccountType(oauthAccountType);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }
    /**
     * 修改
     * @param oauthAccountType
     */
    @PutMapping(value="/update")
    @ApiOperation(value="编辑单个账户类型", notes="编辑单个账户类型")
    public BaseResult updateOauthAccountType(@RequestBody OauthAccountType oauthAccountType){
        int i = 0;
        if(null != oauthAccountType){
            i=oauthAccountTypeService.updateOauthAccountType(oauthAccountType);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除账户类型", notes="删除账户类型")
    public BaseResult delAccountType(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){

            OauthAccountType oauthAccountType = oauthAccountTypeService.getOauthAccountTypeById(id);
            i=oauthAccountTypeService.delAccountType(oauthAccountType);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 查询全部账户类型
     */
    @ApiOperation(value="查询全部账户类型", notes="查询全部账户类型")
    @GetMapping(value="/listAll")
    @Auth(value = "/oauthAccountType/listAll",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<OauthAccountType>> listAll(OauthAccountTypeForm oauthAccountTypeForm){
        List<OauthAccountType> oauthAccountTypeList = oauthAccountTypeService.getOauthAccountTypeList(oauthAccountTypeForm);
        return outDataStr(oauthAccountTypeList);
    }

    /**
     * 根据角色id查询全部账户类型
     * @param roleId
     * @return
     */
    @ApiOperation(value="根据角色id查询全部账户类型", notes="根据角色id查询全部账户类型")
    @GetMapping(value="/list/{roleId}")
    @Auth(value = "/oauthAccountType/list/role",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<OauthAccountType>> getRoleAccountTypeListByRoleId(@PathVariable("roleId")String roleId){
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId);
        StringBuilder accountTypeIds = new StringBuilder();
        List<OauthAccountType> oauthAccountTypeList = oauthAccountTypeService.getAccountTypeListByRoleId(map);
        return outDataStr(oauthAccountTypeList);
    }
}
